<?php

/**
 * FILE: functions.php 
 * Created on Jul 14, 2012 at 1:44:01 PM 
 * Author: Mr.Vibe 
 * Credits: www.VibeThemes.com
 * Project: WordPress 
 * License: GPLv2
 */

define('VIBE_URL',get_stylesheet_directory_uri());

if ( function_exists( 'add_image_size' ) ) { 
	add_image_size( 'category', 528, 260 ); //300 pixels wide 
	add_image_size( 'featured', 252, 237);
}

set_post_thumbnail_size( 252, 237 ); 

add_theme_support( 'post-thumbnails' );

if(function_exists('register_sidebar'))
{
    register_sidebar( array(
		'name' => 'MainSidebar',
		'id' => 'mainsidebar',
		'before_widget' => '<div class="block">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="widget-title">',
		'after_title' => '</h4>'
	) );
    register_sidebar( array(
		'name' => 'Footer Left Sidebar',
		'id' => 'footerleftsidebar',
		'before_widget' => '<div class="widget-block">',
		'after_widget' => '</div>',
		'before_title' => '<h4>',
		'after_title' => '</h4>'
	) );
    register_sidebar( array(
		'name' => 'Footer Middle Sidebar',
		'id' => 'footermiddlesidebar',
		'before_widget' => '<div class="widget-block">',
		'after_widget' => '</div>',
		'before_title' => '<h4>',
		'after_title' => '</h4>'
	) );
    register_sidebar( array(
		'name' => 'Footer Right Sidebar',
		'id' => 'footerrightsidebar',
		'before_widget' => '<div class="widget-block">',
		'after_widget' => '</div>',
		'before_title' => '<h4>',
		'after_title' => '</h4>'
	) );
}

add_action( 'init', 'register_vibe_menus' );
function register_vibe_menus() {
register_nav_menus(
array(
'main-menu' => __( 'Main Menu' )
  )
  );
}

add_action('wp_print_scripts', 'vibe_scripts');

function vibe_scripts() {
    //wp_deregister_script('jquery' );
    //wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
    
    wp_register_script( 'custom_js', VIBE_URL.'/js/custom.js');
    wp_register_script( 'tabs_js', VIBE_URL.'/js/tabs.js');
    // -- Masonry --
    wp_register_script( 'masonry_js', VIBE_URL.'/js/masonry.min.js');
    wp_register_script( 'imagesloaded_js', VIBE_URL.'/js/imagesloaded.js');
    // -- Tweet --
    wp_register_script( 'tweet_js', VIBE_URL.'/js/tweet/jquery.tweet.js');
    
    //-- superfish --
    wp_register_script( 'hoverintent_js', VIBE_URL.'/js/superfish-1.4.8/js/hoverIntent.js');
    wp_register_script( 'superfish_js', VIBE_URL.'/js/superfish-1.4.8/js/superfish.js');
    wp_register_script( 'supersubs_js', VIBE_URL.'/js/superfish-1.4.8/js/supersubs.js');
    
    //prettyphoto
    wp_register_script( 'prettyphoto_js', VIBE_URL.'/js/prettyPhoto/js/jquery.prettyPhoto.js');
    //-- poshytip --
    wp_register_script( 'poshytip_js', VIBE_URL.'/js/poshytip-1.1/src/jquery.poshytip.min.js');
    wp_register_script( 'lessgrid_js', VIBE_URL.'/js/less-grid-4.js');
    wp_register_script( 'modernizr_js', VIBE_URL.'/js/modernizr.js');
    wp_register_script( 'flexslider_js', VIBE_URL.'/js/jquery.flexslider-min.js');


                
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'custom_js' );
    wp_enqueue_script( 'tabs_js' );
    wp_enqueue_script( 'masonry_js' );
    wp_enqueue_script( 'imagesreloaded_js' );
    wp_enqueue_script( 'tweet_js' );
    wp_enqueue_script( 'hoverintent_js' );
    wp_enqueue_script( 'superfish_js' );
    wp_enqueue_script( 'supersubs_js' );
    wp_enqueue_script( 'prettyphoto_js' );
    wp_enqueue_script( 'poshytip_js' );
    wp_enqueue_script( 'lessgrid_js' );
    wp_enqueue_script( 'flexslider_js' );
    wp_enqueue_script( 'modernizr_js' );
}     

add_action('wp_print_styles', 'vibe_styles');

function vibe_styles() {
    wp_register_style('tweet_css', VIBE_URL.'/css/jquery.tweet.css');
    wp_register_style('superfish_css', VIBE_URL.'/css/superfish.css');
    wp_register_style('prettyphoto_css', VIBE_URL.'/js/prettyPhoto/css/prettyPhoto.css');
    wp_register_style('poshytip_css', VIBE_URL.'/js/poshytip-1.1/src/tip-twitter/tip-twitter.css');
    wp_register_style('yellowtip_css', VIBE_URL.'/js/poshytip-1.1/src/tip-yellowsimple/tip-yellowsimple.css');
    wp_register_style('lessgrid_css', VIBE_URL.'/css/lessgrid.css');
    wp_register_style('flexslider_css', VIBE_URL.'/css/flexslider.css');
	
    
    wp_enqueue_style('tweet_css');
    wp_enqueue_style('superfish_css');
    wp_enqueue_style('prettyphoto_css');
    wp_enqueue_style('poshytip_css');
    wp_enqueue_style('yellowtip_css');
    wp_enqueue_style('flexslider_css');
    wp_enqueue_style('lessgrid_css');
}

function list_pages(){
wp_list_pages("title_li=");
}

function list_cats_with_desc() {
  $base = wp_list_categories('echo=0&orderby=ID&use_desc_for_title=1&title_li=');
 
  // wp_list_categories adds a "cat-item-[category_id]" class to the <li> so let's make use of that!
  // Shouldn't really use regexp to parse HTML, but oh well.
  // (for the curious, here's why: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )
 
  $get_cat_id = '/cat-item-[0-9]+/';
  preg_match_all($get_cat_id, $base, $cat_id);
 
  // Let's prepare our category descriptions to be injected.
  // Format will be <a>category-name<span>category-desc</span></a>
  $inject_desc = array();
 
  $i = 0;
  foreach($cat_id[0] as $id) {
    $id = trim($id,'cat-item-');
    $id = trim($id,'"');
 
    $desc = trim(strip_tags(category_description($id)),"\n");   // For some reason, category_description returns the
                                                                // description wrapped in an unwanted paragraph tag which
                                                                // we remove with strip_tags. It also adds a newline
                                                                // which we promptly trim out.
    if($desc=="") $desc = "Add Description";
 
    $inject_desc[$i] = '<span class="subheader">' . $desc . '</span></a>';
    $i++;
  }
 
  // Now we inject our descriptions
  $base_arr = explode("\n", $base);
 
  $base_i = 0;
  foreach($inject_desc as $desc) {
 
    // We check whether there's an occurence of "</a>"
    while(strpos($base_arr[$base_i], "</a>") === false) {
      $base_i++;
    }
 
    // If we find one, inject our description <span>
    $base_arr[$base_i] = str_replace("</a>", $desc, $base_arr[$base_i]);
 
    $base_i++;
  }
 
  $base = implode("\n", $base_arr);
  echo ' 
  <li><a href="'.site_url().'" title="Home">Home<span class="subheader">it all begins here</span></a></li>';
  echo $base;
}

// Removes ul class from wp_nav_menu
function remove_tags ( $menu ){
    preg_replace( array( '#^<div[^>]*>#', '#</div>$#' ), '', $menu );
    return preg_replace( array( '#^<ul[^>]*>#', '#</ul>$#' ), '', $menu );
}
add_filter( 'wp_nav_menu', 'remove_tags' );

class description_walker extends Walker_Nav_Menu
{
      function start_el(&$output, $item, $depth, $args)
      {
           global $wp_query;
           $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

           $class_names = $value = '';

           $classes = empty( $item->classes ) ? array() : (array) $item->classes;

           $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
           $class_names = ' class="sf-menu '. esc_attr( $class_names ) . '"';

           $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

           $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
           $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
           $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
           $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

           $prepend = '';
           $append = '';
           $description  = ! empty( $item->title ) ? '<span class="subheader">'.esc_attr( $item->title ).'</span>' : '<span class="subheader">Add title</span>';

           if($depth != 0)
           {
                     $description = $append = $prepend = "";
           }

            $item_output = $args->before;
            $item_output .= '<a'. $attributes .'>';
            $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
            $item_output .= $description.$args->link_after;
            $item_output .= '</a>';
            $item_output .= $args->after;

            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
            }
}

/*=====================================Pagination Thanks to Kreisi===================*/
function pagination($pages = '', $range = 2)
{  
     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   
     
/*<ul class="pager">
					<li class="paged">Page 1 of 2</li>
					<li class="active"><a href="#">1</a></li>
					<li><a href="#">2</a></li>
					<li><a href="#">3</a></li>
				</ul> */
     if($pages)
     {
         echo "<ul class='pager'><li class='paged'>Page 1 of $pages </li>";
        
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<li class='active'>".$i."</li>":"<li><a href='".get_pagenum_link($i)."' >".$i."</a></li>";
             }
         }

          echo "</li>";
     }
}

function get_imagesrc($type){
    global $post;
    $imgs = get_the_post_thumbnail($post->ID,$type);
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $imgs, $matches);
    return($matches [1] [0]);
}




function vibe_excerpt() {
       global $post;
	return '<a href="'. get_permalink($post->ID) . '" class="readmore"> Read more</a>';
}
add_filter('excerpt_more', 'vibe_excerpt');

function the_portfolio_excerpt($chars) {
       global $post;

	$text = get_the_content();

	$text = $text . " ";
	$text = strip_tags($text);

	if( strlen($text) > $chars )
		$ellipsis = true;

	$text = substr($text,0,$chars);

	$text = substr($text,0,strrpos($text,' '));

	if( $ellipsis == true )
		$text = $text . "...";

	echo $text;
}

function the_breadcrumb() {
	if (!is_home()) {
		echo '<li><a href="';
		echo get_option('home');
		echo '">';
		bloginfo('name');
		echo "</a></li><li> / ";
		if (is_category() || is_single()) {
			the_category(' / ','');
			if (is_single()) {
				echo " / ";
				the_title();
			}
		} elseif (is_page()) {
			echo the_title();
		}
                echo "</li>";
	}
}

get_template_part('vibe', 'options');

//Widgets
//get_template_part('vibe', 'widgets');
?>
